<template>
  <el-dialog
    title="添加动态分类"
    :visible.sync="visible"
    width="400px"
    :before-close="
      () => {
        $emit('close');
      }
    "
  >
    <el-form
      size="small"
      :rules="rules"
      :model="form"
      ref="form"
      label-width="80px"
    >
      <el-form-item label="名称" prop="dynamic_name">
        <el-input v-model="form.dynamic_name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { setDynamicClass } from "@/api/dynamic-mng";
export default {
  props: ["visible"],
  data() {
    return {
      form: {
        dynamic_name: null
      },
      rules: {
        dynamic_name: [
          { required: true, message: "请填写分类名", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    visible: function(val) {
      if (val) {
      } else {
        this.$refs["form"].resetFields();
      }
    }
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          setDynamicClass({ dynamic_name: this.form.dynamic_name }).then(
            res => {
              if (res.code === 200) {
                this.$message({
                  message: "添加成功",
                  type: "success"
                });
                this.$emit("close");
              }
            }
          );
        }
      });
    }
  },
  created() {}
};
</script>

<style></style>
